package com.pine.sort.impl;

import com.alibaba.fastjson.JSON;
import com.pine.common.GeneratorArr;
import com.pine.sort.ISort;

/**
 * 插入排序类
 */
public class InsertSort implements ISort {
    @Override
    public <E extends Comparable<E>> E[] sort(E[] eArr) {
        for (int i = 1; i < eArr.length; i++) {
            int tem = i;
            for (int j = i-1; j > -1; j--) {
                if(eArr[j].compareTo(eArr[tem]) < 0) {
                    break;
                }
                ISort.swap(eArr, j, tem);
                tem -= 1;
            }
        }
        return eArr;
    }

    public static void main(String[] args) {
        InsertSort insertSort = new InsertSort();
        int len = 10;
        Integer[] intArr = GeneratorArr.outOfOrderDistinctIntegerArr(len);
        System.out.println(JSON.toJSONString(intArr));
        insertSort.sort(intArr);
        System.out.println(JSON.toJSONString(intArr));
    }
}
